package org.eleorc.utils;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ApplicationContextUtils {

    public static void printBeanDefinitionInfo(ConfigurableListableBeanFactory factory){
        System.out.println(factory.getClass()+" has beandefinitions ===========>");
        for (String beanDefinitionName : factory.getBeanDefinitionNames()) {
            System.out.print(beanDefinitionName+":===>");
            System.out.println(factory.getBeanDefinition(beanDefinitionName));
        }
        System.out.println(factory.getClass()+" has beandefinitions <===========");
    }


    public static void printBeanDefinitionNames(ApplicationContext context){
        System.out.println(context+" has beandefinitions ===========>");
        Arrays.stream(context.getBeanDefinitionNames()).forEach(System.out::println);
        System.out.println(context+" has beandefinitions <===========");
    }

    public static List<BeanDefinition> getBeanDefinitions(ConfigurableListableBeanFactory factory){
        ArrayList<BeanDefinition> beanDefinitions = new ArrayList<>();
        String[] beanDefinitionNames = factory.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            BeanDefinition beanDefinition = factory.getBeanDefinition(beanDefinitionName);
            beanDefinitions.add(beanDefinition);
        }
        return beanDefinitions;
    }



}
